package com.jgx.dao;
import com.jgx.bean.Finance509;
import com.jgx.util.Dbutil509;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FinanceDao509 {
    public List<Finance509> findAll() {
        Connection connection = Dbutil509.getConnection();
        if (connection == null) {
            System.out.println("数据库连接失败");
            return new ArrayList<>(); // 返回空列表，表示连接失败
        }
        List<Finance509> financeList = new ArrayList<>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = "select * from finance";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Finance509 finance = new Finance509();
                finance.setFinanceID(resultSet.getInt(1));
                finance.setCustomerID(resultSet.getInt(2));
                finance.setTrp(resultSet.getString(3));
                finance.setTbp(resultSet.getString(4));


                financeList.add(finance);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Dbutil509.closeAll(resultSet, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for(Finance509 finance : financeList) {
            System.out.println(finance.toString());
        }
        return financeList;
    }

    public void deleteByid(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "delete from finance where FinanceID = ?";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                Dbutil509.closeAll(null, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void add(Finance509 finance)  {
        Connection connection = null;
        PreparedStatement statement =null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "INSERT INTO finance(CustomerID,TotalRoomPrice,TotalBreakfastPrice) VALUES(?,?,?)";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,finance.getCustomerID());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2, finance.getTrp());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,finance.getTbp());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void update(Finance509 finance){
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "UPDATE finance SET CustomerID=?,TotalRoomPrice=?,TotalBreakfastPrice=? where FinanceID=?" ;
        try {
            statement=connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(4,finance.getFinanceID());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,finance.getCustomerID());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2,finance.getTrp());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,finance.getTbp());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
